﻿using Api1.Util;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Xml;

namespace Api1.Service
{
    /// <summary>
    /// 安科接口服务类
    /// </summary>
    public class AKApiService
    {
        public static AKApiService instance;
        /// <summary>
        /// 会员信息接口访问地址
        /// </summary>
        public static String url = OperateIniFile.ReadIniData("ANKE","Url","");
        public static String url_get_contract_info = "/GetContractInfo";//获取合同号
        public static String url_check_in =  "/CheckIn";//入场
        public static String url_check_out= "/ConfirmCheckOut";//出场


        /// <summary>
        /// 私教打卡url
        /// </summary>
        public static String url1 = OperateIniFile.ReadIniData("ANKE", "Url1", "");
        public static String url1_pt_list = "/GetPTEmployeeList";//私教列表
        public static String url1_re_list = "/GetReservationList";//课程列表
        public static String url1_start = "/DoLessonStart";//开始上课
        public static String url1_end = "/DoLessonEnd";//结束上课

        ///品牌id
        public static String brand_code= OperateIniFile.ReadIniData("ANKE", "BrandCode", "");
        /// 俱乐部id
        public static String club_id = OperateIniFile.ReadIniData("ANKE", "ClubID", "");
        /// 加密键值
        public static String key = OperateIniFile.ReadIniData("ANKE", "Key", "");

        public AKApiService() {
        }
        public static AKApiService GetInstance() {
            if (instance == null) {
                instance = new AKApiService();
            }
            return instance;
        }

        /// <summary>
        ///  根据卡号获取合同编号
        /// </summary>
        /// <param name="Card_No"></param>
        /// <returns></returns>
        public Dictionary<String, Object> GetContractInfo(String Card_No) {
            ///参数
            String param = String.Format("BrandCode={0}&ClubID={1}&CardNo={2}&Key={3}&Mobile=",brand_code,club_id,Card_No,key);
            ///字符串结果 去掉 xml 标签
            String  result = HttpClient.Post(url+url_get_contract_info,param);
            if (String.IsNullOrWhiteSpace(result))
            {
                return HttpClient.Error("配置错误或者卡号不存在ak");
            }
            try {
                List<Dictionary<String, String>>  list=JsonConvert.DeserializeObject<List<Dictionary<String, String>>>(result);
                if (list == null || list.Count < 1)
                {
                    return HttpClient.Error("卡号不存在");
                }                
                Console.WriteLine(list[0]["ContractID"]);
                return HttpClient.Success("成功",list[0]);
            } catch (Exception e) {
                 ApiServiceImpl.WriteLog(typeof(AKApiService),e);                
                 return HttpClient.Error("返回模板解析错误");
            }            
        }
        /// <summary>
        /// 入场操作
        /// </summary>
        /// <param name="ContractID"></param>
        /// <param name="MemberID"></param>
        /// <returns></returns>
        public Dictionary<String, Object> CheckIn(String ContractID,String MemberID) {
            String Param = String.Format("BrandCode={0}&ClubID={1}&ContractID={2}&MemberID={3}&Key={4}", brand_code, club_id, ContractID, MemberID,key);
            ///字符串结果 去掉 xml 标签
            String result = HttpClient.Post(url+url_check_in, Param);
            if (String.IsNullOrWhiteSpace(result))
            {
                return HttpClient.Error("合同编号或者会员编号错误");
            }
            if (result.Equals("1"))
            {
                return HttpClient.Success("成功");
            }
            else {
                return HttpClient.Error(getError(result));
            }
           
        }
        /// <summary>
        /// 出场操作
        /// </summary>
        /// <param name="CardNo"></param>
        /// <returns></returns>
        public Dictionary<String, Object> CheckOut(String CardNo) {
            String Param = String.Format("BrandCode={0}&ClubID={1}&CardNo={2}&Key={3}&Mobile=", brand_code, club_id, CardNo, key);
            ///字符串结果 去掉 xml 标签
            String result = HttpClient.Post(url+url_check_out, Param);
            if (String.IsNullOrWhiteSpace(result))
            {
                return HttpClient.Error("合同编号或者会员编号错误");
            }
            if (result.Equals("1"))
            {
                return HttpClient.Success("成功");
            }
            else
            {
                return HttpClient.Error(getError(result));
            }
        }
        
        /////////////////////////////////////////////////////////////////////////////////////////////////////
        public static Dictionary<String, String> Error = new Dictionary<String, String>{
            { "1","成功(anke)"},
            { "-1","系统错误(anke)"},
            { "-2","参数错误(anke)"},
            { "-3","此卡为外店卡，不能再本店通用(anke)"},
            { "-4","剩余次数为0(anke)"},
            { "-5","已超过该会员携带的人数(anke)"},
            { "-6","主卡不在场，副卡无法入场(anke)"},
            { "-7","高峰时段卡，目前禁止入场，无法入场(anke)"},
            { "-8","当日利用次数已经完，无法入场(anke)"},
            { "-9","本周利用次数已经完，无法入场(anke)"},
            { "-10","本月利用次数已经完，无法入场(anke)"},
            { "-11","入场类商品不存在，无法入场(anke)"},
            { "-12","储值卡余额不足，无法入场(anke)"},
        };
        /// <summary>
        /// 获取安科错误代码
        /// </summary>
        public static String  getError(String code) {
            if (String.IsNullOrWhiteSpace(code))
            {
                return "无效的错误代码";
            }
            var errorstr = Error[code];
            if (String.IsNullOrWhiteSpace(errorstr))
            {
                return "无效的错误代码";
            }
            else {
                return errorstr;
            }
        }
        /// <summary>
        /// 测试
        /// </summary>
        public  String TestGetContractInfo(String Card_No) {
            ///参数
            String param = String.Format("BrandCode={0}&ClubID={1}&CardNo={2}&Key={3}&Mobile=", brand_code, club_id, Card_No, key);
            ///字符串结果 去掉 xml 标签
            String result = HttpClient.TestPost(url+url_get_contract_info, param);
            return result;
        }
        /// <summary>
        /// 测试
        /// </summary>
        /// <param name="ContractID"></param>
        /// <param name="MemberID"></param>
        /// <returns></returns>
        public String TestCheckIn(String ContractID, String MemberID)
        {
            String Param = String.Format("BrandCode={0}&ClubID={1}&ContractID={2}&MemberID={3}&Key={4}", brand_code, club_id, ContractID, MemberID, key);
            ///字符串结果 去掉 xml 标签
            String result = HttpClient.TestPost(url+url_check_in, Param);
            return result;
         }

        public String TestCheckOut(String CardNo)
        {
            String Param = String.Format("BrandCode={0}&ClubID={1}&CardNo={2}&Key={3}&Mobile=", brand_code, club_id, CardNo, key);
            ///字符串结果 去掉 xml 标签
            String result = HttpClient.TestPost(url+url_check_out, Param);
            return result;
        }
        /// <summary>
        /// xml 转 ListDic
        /// </summary>
        public List<Dictionary<String, String>> Xml2ListDic(String xml) {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);
            List<Dictionary<String, String>>  list=JsonConvert.DeserializeObject<List<Dictionary<String, String>>>(doc.InnerText);
            if (list!=null)
            {
                foreach (var item in list)
                {
                    Console.WriteLine(item["CardNo"]);
                }
               return  list;
            }

            return null;
        }

    }
}
